// mmo_game_server/logging/prometheus_metrics.go
package logging

import (
    "net/http"

    "github.com/prometheus/client_golang/prometheus"
    "github.com/prometheus/client_golang/prometheus/promhttp"
)

// 定义指标
var (
    requestsTotal = prometheus.NewCounterVec(
        prometheus.CounterOpts{
            Name: "http_requests_total",
            Help: "Total number of HTTP requests",
        },
        []string{"method", "path"},
    )
    responseTime = prometheus.NewHistogramVec(
        prometheus.HistogramOpts{
            Name:    "http_response_time_seconds",
            Help:    "HTTP response time in seconds",
            Buckets: prometheus.DefBuckets,
        },
        []string{"method", "path"},
    )
)

// InitMetrics 初始化指标
func InitMetrics() {
    prometheus.MustRegister(requestsTotal)
    prometheus.MustRegister(responseTime)
    http.Handle("/metrics", promhttp.Handler())
    go func() {
        log.Fatal(http.ListenAndServe(":9090", nil))
    }()
}

// RecordRequest 记录请求
func RecordRequest(method, path string) {
    requestsTotal.WithLabelValues(method, path).Inc()
}

// RecordResponseTime 记录响应时间
func RecordResponseTime(method, path string, duration float64) {
    responseTime.WithLabelValues(method, path).Observe(duration)
}